package cn.smbms.controller;

import cn.smbms.pojo.Bill;
import cn.smbms.pojo.Provider;
import cn.smbms.pojo.User;
import cn.smbms.service.bill.BillService;
import cn.smbms.service.provider.ProviderService;
import cn.smbms.tools.Constants;
import cn.smbms.tools.PageSupport;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.mysql.jdbc.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

@Controller
@RequestMapping("/sys/bill")
public class BillController {
    @Resource
    private BillService billService;
    @Resource
    private ProviderService providerService;
    @RequestMapping(value = "/list.html")
    public String getBillList(Model model,
                              @RequestParam(value = "queryProductName", required = false) String queryProductName,
                              @RequestParam(value = "queryProviderId", required = false) String queryProviderId,
                              @RequestParam(value = "queryIsPayment", required = false) String queryIsPayment,
                              @RequestParam(value = "pageIndex", required = false) String pageIndex) {
        int _queryProviderId = 0;
        int _queryIsPayment=0;
        List<Bill> billList = new ArrayList<Bill>();
        List<Provider>providerList=new ArrayList<Provider>();
        //设置页面容量
        int pageSize = Constants.pageSize;
        //当前页码
        int currentPageNo = 1;

        if (queryProductName==null){
            queryProductName="";
        }
        if (queryProviderId !=null && !queryProviderId.equals("")){
            _queryProviderId = Integer.parseInt(queryProviderId);
        }
        if (queryIsPayment !=null && !queryIsPayment.equals("")){
            _queryIsPayment = Integer.parseInt(queryIsPayment);
        }
        if (pageIndex != null) {
            try {
                currentPageNo = Integer.valueOf(pageIndex);
            } catch (NumberFormatException e) {
                return "redirect:/sys/bill/syserror.html";
            }
        }
        //总数量
        int totalCount = 0;
        try {
            totalCount = billService.getBillCount(queryProductName,_queryProviderId,_queryIsPayment);
        } catch (Exception e) {
            e.printStackTrace();
        }
        //总页数
        PageSupport pages =  new PageSupport();
        pages.setCurrentPageNo(currentPageNo);
        pages.setPageSize(pageSize);
        pages.setTotalCount(totalCount);
        int totalPageCount = pages.getTotalPageCount();
        //控制首页和尾页
        if(currentPageNo < 1){
            currentPageNo = 1;
        }else if(currentPageNo > totalPageCount){
            currentPageNo = totalPageCount;
        }
        try {
            billList = billService.getBillList(queryProductName,_queryProviderId,_queryIsPayment,currentPageNo,pageSize);
            providerList=providerService.getProList();
        } catch (Exception e) {
            e.printStackTrace();
        }
        model.addAttribute("providerList",providerList);
        model.addAttribute("billList", billList);
        model.addAttribute("queryProductName", queryProductName);
        model.addAttribute("queryProviderId", queryProviderId);
        model.addAttribute("totalPageCount", totalPageCount);
        model.addAttribute("totalCount", totalCount);
        model.addAttribute("currentPageNo", currentPageNo);
        return "billlist";
    }
    @RequestMapping(value = "/syserror.html")
    public String sysError() {
        return "syserror";
    }
    @RequestMapping(value="/view/{id}",method=RequestMethod.GET)
    public String view(@PathVariable String id,Model model,HttpServletRequest request){
        Bill bill= new Bill();
        try {
            bill = billService.getBillById(Integer.parseInt(id));
        } catch (NumberFormatException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }

        model.addAttribute(bill);
        return "billview";
    }
    @RequestMapping(value="/modify/{id}",method=RequestMethod.GET)
    public String getBillById(@PathVariable String id,Model model,HttpServletRequest request){
        Bill bill = new Bill();
        try {
            bill = billService.getBillById(Integer.parseInt(id));
        } catch (NumberFormatException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        model.addAttribute(bill);
        return "billmodify";
    }
    @RequestMapping(value="/modifysave.html",method=RequestMethod.POST)
    public String modifyBillSave(Bill bill,HttpSession session,HttpServletRequest request){
        bill.setModifyBy(((User)session.getAttribute(Constants.USER_SESSION)).getId());
        bill.setModifyDate(new Date());
            try {
                if(billService.modify(bill)){
                    return "redirect:/sys/bill/list.html";
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        return "billmodify";
        }
    @RequestMapping(value="/billsaveadd.html",method=RequestMethod.POST)
    public String addBillSave(Bill bill,HttpSession session,HttpServletRequest request) throws Exception {
        bill.setCreatedBy(((User)session.getAttribute(Constants.USER_SESSION)).getId());
        bill.setCreationDate(new Date());
        if(billService.add(bill)){
                return "redirect:/sys/bill/list.html";
            }
        return "billadd";
    }


    @RequestMapping("/billadd.html")
    public  String bill(){
        return "billadd";
    }










    //用户删除
    @RequestMapping(value="/delbill",method=RequestMethod.GET)
    @ResponseBody
    public Object delbill(@RequestParam String id) throws Exception {
        HashMap<String, String> resultMap = new HashMap<String, String>();
        if(StringUtils.isNullOrEmpty(id)){
            resultMap.put("delResult", "notexist");
        }else{
            if(billService.deleteBillById(Integer.parseInt(id)))
                resultMap.put("delResult", "true");
            else
                resultMap.put("delResult", "false");
        }
        return JSONArray.toJSONString(resultMap);
    }



    //用户列表显示
    @RequestMapping(value="/providerlist",method=RequestMethod.GET,produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    public Object getBillList(){
        List<Provider> providerlist = null;
        try {
            providerlist=providerService.getProList();
            System.out.println("错了");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return JSONArray.toJSONString(providerlist);
    }
}